package com.itany.shop.servlet;

import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 石小俊
 */
@WebServlet(urlPatterns = "*.cart")
public class CartServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");
        String path = req.getServletPath();
        switch (path) {
            case "/add.cart":
                addCart(req, resp);
                break;
            case "/modify.cart":
                modifyCart(req, resp);
                break;
            case "/deleteById.cart":
                deleteById(req, resp);
                break;
            case "/deleteByIds.cart":
                deleteByIds(req, resp);
                break;
            case "/clean.cart":
                cleanCart(req, resp);
                break;
            default:
                PrintWriter out = resp.getWriter();
                out.println("<h1 style='color:red;'>对不起,您访问的资源有误,请检查请求是否正确</h1>");
        }
    }

    protected void addCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        // 获取请求中的参数
        String id = req.getParameter("id");
        // 获取会话中的购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        try {
            cartService.addCart(cart, id);
            resp.sendRedirect(req.getContextPath() + "/findAll.product");
        } catch (UserNotLoginException e) {
            req.setAttribute("loginMsg",  e.getMessage());
            req.getRequestDispatcher("/jsp/login.jsp").forward(req, resp);
        } catch (RequestParameterErrorException e) {
            req.setAttribute("productMsg", e.getMessage());
            req.getRequestDispatcher("/findAll.product").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("productMsg", "服务器内部异常");
            req.getRequestDispatcher("/findAll.product").forward(req, resp);
        }

    }

    protected void modifyCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        String id = req.getParameter("id");
        String num = req.getParameter("num");
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        try {
            cartService.modifyCart(cart, id, num);
            resp.sendRedirect(req.getContextPath() + "/jsp/cart.jsp");
        } catch (UserNotLoginException e) {
            req.setAttribute("loginMsg", e.getMessage());
            req.getRequestDispatcher("/jsp/login.jsp").forward(req, resp);
        } catch (RequestParameterErrorException e) {
            req.setAttribute("cartMsg", e.getMessage());
            req.getRequestDispatcher("/jsp/cart.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("cartMsg", "服务器内部异常");
            req.getRequestDispatcher("/jsp/cart.jsp").forward(req, resp);
        }
    }

    protected void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        String id = req.getParameter("id");
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        try {
            cartService.removeCart(cart, id);
            resp.sendRedirect(req.getContextPath() + "/jsp/cart.jsp");
        } catch (UserNotLoginException e) {
            req.setAttribute("loginMsg", e.getMessage());
            req.getRequestDispatcher("/jsp/login.jsp").forward(req, resp);
        } catch (RequestParameterErrorException e) {
            req.setAttribute("cartMsg", e.getMessage());
            req.getRequestDispatcher("/jsp/cart.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("cartMsg", "服务器内部异常");
            req.getRequestDispatcher("/jsp/cart.jsp").forward(req, resp);
        }
    }

    protected void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        String[] ids = req.getParameterValues("productId");
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        try {
            for (String id : ids) {
                cartService.removeCart(cart, id);
            }
            resp.sendRedirect(req.getContextPath() + "/jsp/cart.jsp");
        } catch (UserNotLoginException e) {
            req.setAttribute("loginMsg", e.getMessage());
            req.getRequestDispatcher("/jsp/login.jsp").forward(req, resp);
        } catch (RequestParameterErrorException e) {
            req.setAttribute("cartMsg", e.getMessage());
            req.getRequestDispatcher("/jsp/cart.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("cartMsg", "服务器内部异常");
            req.getRequestDispatcher("/jsp/cart.jsp").forward(req, resp);
        }
    }

    protected void cleanCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 为会话中的购物车对象重新赋值,给一个新的购物车对象
        req.getSession().setAttribute("cart", new Cart());
        resp.sendRedirect(req.getContextPath() + "/jsp/cart.jsp");
    }
}
